package yu.vert.main;

import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.MySQLClient;
import io.vertx.ext.jdbc.JDBCClient;
import io.vertx.ext.sql.ResultSet;
import io.vertx.ext.sql.SQLClient;
import io.vertx.ext.web.Router;

import java.util.List;

public class VertWebMain {

    public static void main(String[] args) {

        Vertx vertx = Vertx.factory.vertx(new VertxOptions().setWorkerPoolSize(40));
        jdbcStart(vertx);
    }

    public static void webStart(Vertx vertx) {

        HttpServer httpServer = vertx.createHttpServer();
        Router router = Router.router(vertx);
        router.route("/a").handler(routingContext -> {
            HttpServerResponse response = routingContext.response();
            response.putHeader("content-type", "text/plain");
            response.end("Hello World from Vert.x-Web!");
        });
        httpServer.requestHandler(router::accept).listen(8080);
    }

    public static void jdbcStart(Vertx vertx) {

        JsonObject config = new JsonObject()
                .put("user", "root")
                .put("password", "123456")
                .put("url", "jdbc:mysql://127.0.0.1:3306/test")
                .put("driver_class", "com.mysql.jdbc.Driver");
        //SQLClient client = MySQLClient.createShared(vertx, config);
        SQLClient client = JDBCClient.createNonShared(vertx, config);
        client.query("select * from user", res -> {
            if(res.succeeded()){
                ResultSet result = res.result();
                List<JsonArray> results = result.getResults();
                System.out.println(result.toJson());
            }
        });
    }

}
